﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool.CODE_Chris.DataSourceHelpers
{
    public class IpAdressHelper
    {
        private IpDevices parent;
        private GeneralHelper helper;
        private SQL sql;

        private DataGridView gridView1;

        private SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();

        private BindingSource bindingSource1 = new BindingSource();

        private DataTable dataTable1;

        private String select1;
        private String update1;

        private Boolean isPaste = false;
        private int countPasteError = 0;

        public IpAdressHelper(DataGridView gridView1, IpDevices parent)
        {
            this.parent = parent;
            this.gridView1 = gridView1;
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new GeneralHelper();

            populateSQLStrings();
        }

        #region Methoden für gridView1 (Hauptmaske)
        public void getData1()
        {
            try
            {
                gridView1.DataSource = bindingSource1;

                sqlDataAdapter1 = new SqlDataAdapter(select1, Login.connection);
                sqlDataAdapter1.UpdateCommand = new SqlCommand(update1, new SqlConnection(Login.connection));

                //Neu zu schreibende Parameter
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@supplier", SqlDbType.NVarChar, 50, "Supplier");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@rack", SqlDbType.NVarChar, 50, "Rack");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@futureUse1", SqlDbType.NVarChar, -1, "IP Adress Interface 2");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@futureUse2", SqlDbType.NVarChar, -1, "IP Adress Interface 3");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@remarksDevice", SqlDbType.NVarChar, -1, "Remarks Device");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@deviceID", SqlDbType.Int, 10, "Device ID");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@adressType", SqlDbType.NVarChar, 50, "Adress Type");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@ipAdress", SqlDbType.NVarChar, 50, "IP Adress Interface 1");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@ipMask", SqlDbType.NVarChar, 50, "IP Mask");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@gateway", SqlDbType.NVarChar, 50, "Gateway");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@netID", SqlDbType.NVarChar, 50, "Net Adress");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@purpose", SqlDbType.NVarChar, 50, "Purpose");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@trafficType", SqlDbType.NVarChar, 50, "Traffic Type");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@nextHop", SqlDbType.NVarChar, 50, "Future Use");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@nextPort", SqlDbType.NVarChar, 50, "Password");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@logDestination", SqlDbType.NVarChar, 50, "IP Adress Interface 4");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@destinationIP", SqlDbType.NVarChar, 50, "IP Adress Interface 5");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@ifType", SqlDbType.NVarChar, 50, "Interface Type");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@ifNumber", SqlDbType.NVarChar, 50, "Port");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@remarksIF", SqlDbType.NVarChar, -1, "Remarks");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@updateDate", SqlDbType.Date, 10, "Date");
                sqlDataAdapter1.UpdateCommand.Parameters.Add("@updateUser", SqlDbType.NVarChar, 50, "User");

                //Daten aus der Originalquelle
                SqlParameter parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_id", SqlDbType.Int, 50, "id");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_supplier", SqlDbType.NVarChar, 50, "Supplier");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_rack", SqlDbType.NVarChar, 50, "Rack");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_futureUse1", SqlDbType.NVarChar, -1, "IP Adress Interface 2");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_futureUse2", SqlDbType.NVarChar, -1, "IP Adress Interface 3");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_remarksDevice", SqlDbType.NVarChar, -1, "Remarks Device");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_deviceID", SqlDbType.Int, 50, "Device ID");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_adressType", SqlDbType.NVarChar, 10, "Adress Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_ipAdress", SqlDbType.NVarChar, 50, "IP Adress Interface 1");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_ipMask", SqlDbType.NVarChar, 50, "IP Mask");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_gateway", SqlDbType.NVarChar, 50, "Gateway");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_netID", SqlDbType.NVarChar, 50, "Net Adress");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_purpose", SqlDbType.NVarChar, 50, "Purpose");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_trafficType", SqlDbType.NVarChar, 50, "Traffic Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_nextHop", SqlDbType.NVarChar, 50, "Future Use");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_nextPort", SqlDbType.NVarChar, 50, "Password");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_logDestination", SqlDbType.NVarChar, 50, "IP Adress Interface 4");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_destinationIP", SqlDbType.NVarChar, 50, "IP Adress Interface 5");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_ifType", SqlDbType.NVarChar, 50, "Interface Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_ifNumber", SqlDbType.NVarChar, 50, "Port");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_remarksIF", SqlDbType.NVarChar, -1, "Remarks");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_updateDate", SqlDbType.Date, 10, "Date");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@Original_updateUser", SqlDbType.NVarChar, 50, "User");
                parameter.SourceVersion = DataRowVersion.Original;

                //Check auf Null-Werte
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_supplier", SqlDbType.Int, 50, "Supplier");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_rack", SqlDbType.Int, 10, "Rack");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_futureUse1", SqlDbType.Int, 50, "IP Adress Interface 2");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_futureUse2", SqlDbType.Int, 10, "IP Adress Interface 3");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_remarksDevice", SqlDbType.Int, 50, "Remarks Device");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_deviceID", SqlDbType.Int, 10, "Device ID");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_adressType", SqlDbType.Int, 50, "Adress Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_ipAdress", SqlDbType.Int, 10, "IP Adress Interface 1");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_ipMask", SqlDbType.Int, 50, "IP Mask");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_gateway", SqlDbType.Int, 10, "Gateway");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_netID", SqlDbType.Int, 50, "Net Adress");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_purpose", SqlDbType.Int, 10, "Purpose");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_trafficType", SqlDbType.Int, 50, "Traffic Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_nextHop", SqlDbType.Int, 10, "Future Use");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_nextPort", SqlDbType.Int, 50, "Password");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_logDestination", SqlDbType.Int, 10, "IP Adress Interface 4");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_destinationIP", SqlDbType.Int, 50, "IP Adress Interface 5");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_ifType", SqlDbType.Int, 50, "Interface Type");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_ifNumber", SqlDbType.Int, 50, "Port");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_remarksIF", SqlDbType.Int, 50, "Remarks");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_updateDate", SqlDbType.Int, 50, "Date");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;
                parameter = sqlDataAdapter1.UpdateCommand.Parameters.Add("@IsNull_updateUser", SqlDbType.Int, 50, "User");
                parameter.SourceVersion = DataRowVersion.Original;
                parameter.SourceColumnNullMapping = true;

                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sqlDataAdapter1);

                // Populate a new data table and bind it to the BindingSource.
                dataTable1 = new DataTable();
                dataTable1.Locale = System.Globalization.CultureInfo.InvariantCulture;
                sqlDataAdapter1.Fill(dataTable1);
                bindingSource1.DataSource = dataTable1;

                gridView1.AutoGenerateColumns = false;

                //Zellennachbearbeitung
                setFixedColumns1();
                setHiddenColumns1();
                setDisabledColumns1();

                //Eventhandler setzen
                gridView1.CellFormatting -= new DataGridViewCellFormattingEventHandler(this.gridView1_CellFormatting);
                gridView1.CellEndEdit -= new DataGridViewCellEventHandler(this.gridView1_CellEndEdit);
                gridView1.CellMouseClick -= new DataGridViewCellMouseEventHandler(this.gridView1_CellMouseClick);
                gridView1.CellMouseDown -= new DataGridViewCellMouseEventHandler(this.gridView1_CellMouseDown);
                gridView1.CellValueChanged -= new DataGridViewCellEventHandler(this.gridView1_CellValueChanged);

                gridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(this.gridView1_CellFormatting);
                gridView1.CellEndEdit += new DataGridViewCellEventHandler(this.gridView1_CellEndEdit);
                gridView1.CellMouseClick += new DataGridViewCellMouseEventHandler(this.gridView1_CellMouseClick);
                gridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(this.gridView1_CellMouseDown);
                gridView1.CellValueChanged += new DataGridViewCellEventHandler(this.gridView1_CellValueChanged);
            }
            catch (Exception e)
            {
                sql.log("FEHLER", e, this.ToString() + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }

        private void gridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex != -1 && e.ColumnIndex != -1)
                {
                    gridView1.CurrentCell = gridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
                else
                {
                    gridView1.ClearSelection();
                }
            }
        }

        private void gridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu();

                MenuItem copy = new MenuItem("Copy Value");
                copy.Click += new System.EventHandler(this.copy1_Click);
                m.MenuItems.Add(copy);

                MenuItem paste = new MenuItem("Paste Value");
                paste.Click += new System.EventHandler(this.paste1_Click);
                m.MenuItems.Add(paste);

                MenuItem delete = new MenuItem("Delete Value");
                delete.Click += new System.EventHandler(this.delete1_Click);
                m.MenuItems.Add(delete);

                MenuItem asc = new MenuItem("Insert Ascending IPs");
                asc.Click += new System.EventHandler(this.asc1_Click);
                m.MenuItems.Add(asc);

                MenuItem desc = new MenuItem("Insert Descending IPs");
                desc.Click += new System.EventHandler(this.desc1_Click);
                m.MenuItems.Add(desc);

                var relativeMousePosition = gridView1.PointToClient(Cursor.Position);
                m.Show(gridView1, relativeMousePosition);
            }
        }

        private void asc1_Click(object sender, EventArgs e)
        {
            //Für Korrekte Behandlung der DBConcurrencyException
            isPaste = true;

            List<int> reihen = new List<int> { };
            int spalte = 0;
            int ip = 0;
            int chosenCells = 0;
            string praefix = "";
            string suffix = "";

            chosenCells = gridView1.SelectedCells.Count;
            if (chosenCells > 1)
            {
                spalte = gridView1.SelectedCells[0].ColumnIndex;
                for (int y = 0; y < chosenCells; y++)
                {
                    reihen.Add(gridView1.SelectedCells[y].RowIndex);
                }
                reihen.Sort();
                praefix = gridView1[spalte, reihen[0]].Value.ToString().Substring(0, gridView1[spalte, reihen[0]].Value.ToString().LastIndexOf(".") + 1);
                suffix = gridView1[spalte, reihen[0]].Value.ToString().Substring(gridView1[spalte, reihen[0]].Value.ToString().LastIndexOf(".") + 1);
                if (int.TryParse(suffix, out ip))
                {
                    for (int i = 1; i < chosenCells; i++)
                    {
                        ip++;
                        gridView1[spalte, reihen[i]].Value = praefix + ip.ToString();

                        // Zeile zurück in die DB schreiben
                        gridView1_CellEndEdit(null, null);
                    }

                    if (countPasteError > 0)
                    {
                        if (MessageBox.Show("Der Datensatz wurde in der Zwischenzeit auf dem Server bearbeitet. Es wurden nur die Datensätze aktualisiert, die nicht im Hintergrund bearbeitet wurden. Alle Datensätze werden nun neu geladen.", "Fehler", MessageBoxButtons.OK) == DialogResult.OK)
                        {
                            parent.reloadAllData();
                        }
                    }
                }
            }
            countPasteError = 0;
            isPaste = false;
        }

        private void desc1_Click(object sender, EventArgs e)
        {
            //Für Korrekte Behandlung der DBConcurrencyException
            isPaste = true;

            List<int> reihen = new List<int> { };
            int spalte = 0;
            int ip = 0;
            int chosenCells = 0;
            string praefix = "";
            string suffix = "";

            chosenCells = gridView1.SelectedCells.Count;
            if (chosenCells > 1)
            {
                spalte = gridView1.SelectedCells[0].ColumnIndex;
                for (int y = 0; y < chosenCells; y++)
                {
                    reihen.Add(gridView1.SelectedCells[y].RowIndex);
                }
                reihen.Sort();
                praefix = gridView1[spalte, reihen[0]].Value.ToString().Substring(0, gridView1[spalte, reihen[0]].Value.ToString().LastIndexOf(".") + 1);
                suffix = gridView1[spalte, reihen[0]].Value.ToString().Substring(gridView1[spalte, reihen[0]].Value.ToString().LastIndexOf(".") + 1);
                if (int.TryParse(suffix, out ip))
                {
                    for (int i = 1; i < chosenCells; i++)
                    {
                        ip--;
                        gridView1[spalte, reihen[i]].Value = praefix + ip.ToString();

                        // Zeile zurück in die DB schreiben
                        gridView1_CellEndEdit(null, null);
                    }

                    if (countPasteError > 0)
                    {
                        if (MessageBox.Show("Der Datensatz wurde in der Zwischenzeit auf dem Server bearbeitet. Es wurden nur die Datensätze aktualisiert, die nicht im Hintergrund bearbeitet wurden. Alle Datensätze werden nun neu geladen.", "Fehler", MessageBoxButtons.OK) == DialogResult.OK)
                        {
                            parent.reloadAllData();
                        }
                    }
                }
            }
            countPasteError = 0;
            isPaste = false;
        }

        private void copy1_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView1.SelectedCells.Count == 1 && gridView1.SelectedCells[0].Value.ToString() != "")
                {
                    Clipboard.SetText(gridView1.SelectedCells[0].Value.ToString());
                }
                else if (gridView1.SelectedCells.Count == 1)
                {
                    if (gridView1.SelectedCells[0].OwningColumn.CellType == typeof(DataGridViewComboBoxCell))
                    {
                        if (gridView1.SelectedCells[0].Value.ToString() != "")
                        {
                            Clipboard.SetText(gridView1.SelectedCells[0].Value.ToString());
                        }
                        else
                        {
                            Clipboard.Clear();
                        }
                    }
                    else
                    {
                        Clipboard.Clear();
                    }
                }
                else
                {
                    Clipboard.Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Copy to Clipboard war nicht erfolgreich. Bitte versuchen Sie es erneut.", "Fehler");
            }
        }

        private void paste1_Click(object sender, EventArgs e)
        {
            //Für Korrekte Behandlung der DBConcurrencyException
            isPaste = true;

            List<int> pasteZeilen = new List<int> { };
            string pasteColumn = "";
            if (gridView1.SelectedCells.Count != 0) pasteColumn = gridView1.SelectedCells[0].OwningColumn.Name;

            for (int i = 0; i < gridView1.SelectedCells.Count; i++)
            {
                pasteZeilen.Add(gridView1.SelectedCells[i].OwningRow.Index);
            }

            if (Clipboard.GetText() != "")
            {
                for (int i = 0; i < pasteZeilen.Count; i++)
                {
                    if (!gridView1.SelectedCells[i].OwningColumn.ReadOnly)
                    {
                        try
                        {
                            gridView1.Rows[pasteZeilen[i]].Cells[pasteColumn].Value = Clipboard.GetText();

                            // Zeile zurück in die DB schreiben
                            gridView1_CellEndEdit(null, null);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
                if (countPasteError > 0)
                {
                    if (MessageBox.Show("Der Datensatz wurde in der Zwischenzeit auf dem Server bearbeitet. Es wurden nur die Datensätze aktualisiert, die nicht im Hintergrund bearbeitet wurden. Alle Datensätze werden nun neu geladen.", "Fehler", MessageBoxButtons.OK) == DialogResult.OK)
                    {
                        parent.reloadAllData();
                    }
                }
            }
            countPasteError = 0;
            isPaste = false;
        }

        private void delete1_Click(object sender, EventArgs e)
        {
            //Für Korrekte Behandlung der DBConcurrencyException
            isPaste = true;

            List<int> deleteZeilen = new List<int> { };
            string deleteColumn = "";
            if (gridView1.SelectedCells.Count != 0) deleteColumn = gridView1.SelectedCells[0].OwningColumn.Name;

            for (int i = 0; i < gridView1.SelectedCells.Count; i++)
            {
                deleteZeilen.Add(gridView1.SelectedCells[i].OwningRow.Index);
            }

            if (gridView1.SelectedCells.Count != 0)
            {
                for (int i = 0; i < gridView1.SelectedCells.Count; i++)
                {
                    if (!gridView1.SelectedCells[i].OwningColumn.ReadOnly)
                    {
                        try
                        {
                            if (gridView1.SelectedCells[i].ValueType == typeof(System.DateTime)) gridView1.SelectedCells[i].Value = DBNull.Value;
                            if (gridView1.SelectedCells[i].ValueType == typeof(int)) gridView1.SelectedCells[i].Value = 0;
                            if (gridView1.SelectedCells[i].ValueType == typeof(string)) gridView1.SelectedCells[i].Value = String.Empty;
                            if (gridView1.SelectedCells[i].ValueType == typeof(Boolean)) gridView1.SelectedCells[i].Value = Boolean.FalseString;

                            if (gridView1.SelectedCells[i].ValueType == typeof(System.DateTime) || gridView1.SelectedCells[i].ValueType == typeof(int) || gridView1.SelectedCells[i].ValueType == typeof(string) || gridView1.SelectedCells[i].ValueType == typeof(Boolean))
                            {
                                // Zeile zurück in die DB schreiben
                                gridView1_CellEndEdit(null, null);
                            }
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }

                if (countPasteError > 0)
                {
                    if (MessageBox.Show("Der Datensatz wurde in der Zwischenzeit auf dem Server bearbeitet. Es wurden nur die Datensätze aktualisiert, die nicht im Hintergrund bearbeitet wurden. Alle Datensätze werden nun neu geladen.", "Fehler", MessageBoxButtons.OK) == DialogResult.OK)
                    {
                        parent.reloadAllData();
                    }
                }
            }

            countPasteError = 0;
            isPaste = false;
        }

        private void gridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex != -1)
            {
                DataGridViewColumn col = gridView1.Columns[e.ColumnIndex];

                //Blaue Zellen
                if (col.Name == "Venue" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "System" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Subsystem" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Model" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Manufacturer" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Description" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Position ID" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "Date" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
                if (col.Name == "User" && col.DefaultCellStyle.BackColor != helper.getColors()[0]) col.DefaultCellStyle.BackColor = helper.getColors()[0];
            }
        }

        private void gridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            gridView1.CurrentRow.Cells["Date"].Value = DateTime.Today;
            gridView1.CurrentRow.Cells["User"].Value = Login.user;
        }

        private void gridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //Hier werden die Daten der editierten Zelle wieder in die Datenbank geschrieben
            gridView1.BindingContext[gridView1.DataSource].EndCurrentEdit();
            updateGridView1();
        }

        public Boolean updateGridView1()
        {
            try
            {
                sqlDataAdapter1.Update(dataTable1);
                return true;
            }
            catch (DBConcurrencyException e)
            {
                if (isPaste)
                {
                    countPasteError++;
                }
                else
                {
                    if (MessageBox.Show("Der Datensatz wurde in der Zwischenzeit auf dem Server bearbeitet. Die Datensätze werden neu geladen.", "Fehler", MessageBoxButtons.OK) == DialogResult.OK)
                    {
                        parent.reloadAllData();
                    }
                }

                sql.log("FEHLER", e, this.ToString() + ":" + System.Reflection.MethodBase.GetCurrentMethod().Name);
                return false;
            }
        }

        private void setDisabledColumns1()
        {
            try
            {
                gridView1.Columns["Venue"].ReadOnly = true;
                gridView1.Columns["System"].ReadOnly = true;
                gridView1.Columns["Subsystem"].ReadOnly = true;
                gridView1.Columns["Model"].ReadOnly = true;
                gridView1.Columns["Manufacturer"].ReadOnly = true;
                gridView1.Columns["Description"].ReadOnly = true;
                gridView1.Columns["Position ID"].ReadOnly = true;
                gridView1.Columns["Date"].ReadOnly = true;
                gridView1.Columns["User"].ReadOnly = true;
                gridView1.Columns["PpU"].ReadOnly = true;
            }
            catch (Exception e)
            {

            }
        }

        private void setFixedColumns1()
        {
            try
            {
                gridView1.Columns["Venue"].Frozen = true;
                gridView1.Columns["System"].Frozen = true;
                gridView1.Columns["Subsystem"].Frozen = true;
                gridView1.Columns["Model"].Frozen = true;
                gridView1.Columns["Manufacturer"].Frozen = true;
                gridView1.Columns["Description"].Frozen = true;
                gridView1.Columns["Position ID"].Frozen = true;
            }
            catch (Exception e)
            {

            }
        }

        private void setHiddenColumns1()
        {
            try
            {
                gridView1.Columns["id"].Visible = false;
            }
            catch (Exception e)
            {

            }
        }

        public DataTable getTable1()
        {
            return dataTable1;
        }
        #endregion

        #region SQL-Befehle als Strings
        private void populateSQLStrings()
        {
            select1 = "SELECT Venue, Sparte AS [System], Subsystem, Model, Manufacturer, Discription AS [Description], Positions_ID AS [Position ID], rack AS [Rack], ifNumber AS [Port], ipAdress AS [IP Adress Interface 1], netID AS [Net Adress], ipMask AS [IP Mask], gateway as [Gateway], trafficType AS [Traffic Type], ifType AS [Interface Type], adressType AS [Adress Type], purpose AS [Purpose], futureUse1 AS [IP Adress Interface 2], futureUse2 AS [IP Adress Interface 3], logDestination AS [IP Adress Interface 4], destinationIP AS [IP Adress Interface 5], remarksIF AS [Remarks], deviceID AS [Device ID], supplier AS [Supplier], nextHop AS [Future Use], nextPort AS [Password], remarksDevice AS [Remarks Device], updateDate AS [Date], updateUser AS [User], id FROM vw_eng_IpDevices";
            update1 = "UPDATE tbl_ipDevices SET "
                        + "supplier = @supplier, "
                        + "rack = @rack, "
                        + "futureUse1 = @futureUse1, "
                        + "futureUse2 = @futureUse2, "
                        + "remarksDevice = @remarksDevice, "
                        + "deviceID = @deviceID, "
                        + "adressType = @adressType, "
                        + "ipAdress = @ipAdress, "
                        + "ipMask = @ipMask, "
                        + "gateway = @gateway, "
                        + "netID = @netID, "
                        + "purpose = @purpose, "
                        + "trafficType = @trafficType, "
                        + "nextHop = @nextHop, "
                        + "nextPort = @nextPort, "
                        + "logDestination = @logDestination, "
                        + "destinationIP = @destinationIP, "
                        + "ifType = @ifType, "
                        + "ifNumber = @ifNumber, "
                        + "remarksIF = @remarksIF, "
                        + "updateDate = @updateDate, "
                        + "updateUser = @updateUser "
                    + "WHERE (id = @Original_id) "
                        + "AND (@IsNull_supplier = 1 AND supplier IS NULL OR supplier = @Original_supplier) "
                        + "AND (@IsNull_rack = 1 AND rack IS NULL OR rack = @Original_rack) "
                        + "AND (@IsNull_futureUse1 = 1 AND futureUse1 IS NULL OR futureUse1 = @Original_futureUse1) "
                        + "AND (@IsNull_futureUse2 = 1 AND futureUse2 IS NULL OR futureUse2 = @Original_futureUse2) "
                        + "AND (@IsNull_remarksDevice = 1 AND remarksDevice IS NULL OR remarksDevice = @Original_remarksDevice) "
                        + "AND (@IsNull_deviceID = 1 AND deviceID IS NULL OR deviceID = @Original_deviceID) "
                        + "AND (@IsNull_adressType = 1 AND adressType IS NULL OR adressType = @Original_adressType) "
                        + "AND (@IsNull_ipAdress = 1 AND ipAdress IS NULL OR ipAdress = @Original_ipAdress) "
                        + "AND (@IsNull_ipMask = 1 AND ipMask IS NULL OR ipMask = @Original_ipMask) "
                        + "AND (@IsNull_gateway = 1 AND gateway IS NULL OR gateway = @Original_gateway) "
                        + "AND (@IsNull_netID = 1 AND netID IS NULL OR netID = @Original_netID) "
                        + "AND (@IsNull_purpose = 1 AND purpose IS NULL OR purpose = @Original_purpose) "
                        + "AND (@IsNull_trafficType = 1 AND trafficType IS NULL OR trafficType = @Original_trafficType) "
                        + "AND (@IsNull_nextHop = 1 AND nextHop IS NULL OR nextHop = @Original_nextHop) "
                        + "AND (@IsNull_nextPort = 1 AND nextPort IS NULL OR nextPort = @Original_nextPort) "
                        + "AND (@IsNull_logDestination = 1 AND logDestination IS NULL OR logDestination = @Original_logDestination) "
                        + "AND (@IsNull_destinationIP = 1 AND destinationIP IS NULL OR destinationIP = @Original_destinationIP) "
                        + "AND (@IsNull_ifType = 1 AND ifType IS NULL OR ifType = @Original_ifType) "
                        + "AND (@IsNull_ifNumber = 1 AND ifNumber IS NULL OR ifNumber = @Original_ifNumber) "
                        + "AND (@IsNull_remarksIF = 1 AND remarksIF IS NULL OR remarksIF = @Original_remarksIF) "
                        + "AND (@IsNull_updateDate = 1 AND updateDate IS NULL OR updateDate = @Original_updateDate) "
                        + "AND (@IsNull_updateUser = 1 AND updateUser IS NULL OR updateUser = @Original_updateUser)";


        }
        #endregion
    }
}
